using UnityEngine;
using System.Collections;
using System.Collections.Generic;

//for use in game touch screen control
public class TouchScreenController : MonoBehaviour
{
    public static TouchScreenController instance;
    private Character character;
    public bool pressed = false;    
    public Vector2 pressedPosition;
    public float pressTime;
    public float maxTapDuration = 0.3f;
    public float maxSwipeTime = 0.5f;
    public float maxTapDistance = 10f;
    public float minSwipeDistance = 50f;
    public Camera uiCamera;

//	public Vector2 dashDir = Vector2.zero;

	void Start ()
	{
        instance = this;
        character = GetComponent<Character>();
	}

    public LayerMask layer;
    void FixedUpdate()
    {
//		if(dashDir != Vector2.zero)
//		{
//			OnTouchMove(dashDir);
//			OnTouchMoveEnd(dashDir);
//			dashDir = Vector2.zero;
//		}
//

        Vector2 dir = new Vector2(Input.mousePosition.x - Screen.width/2, Input.mousePosition.y - Screen.height/2);
        if(Input.GetMouseButtonDown(0))
        {
            pressed = true;
            pressTime = Time.time;
            pressedPosition = Input.mousePosition;
        }
        if(Input.GetMouseButtonUp(0))
        {
            Ray ray = uiCamera.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if(Physics.Raycast(ray,out hit,Mathf.Infinity,layer))
            {
                Debug.Log("hit NGUI ui first!" + hit.collider.gameObject);
                return;
            }

            pressed = false;      
            Vector2 move = new Vector2(Input.mousePosition.x - pressedPosition.x, 
                                       Input.mousePosition.y - pressedPosition.y);
            
            if(Time.time - pressTime < maxSwipeTime)
            {
                if(move.magnitude > minSwipeDistance)
                {
					EvadeInput(move);
//					dashDir = dir;
                    return;
                }
            }
            if(Time.time - pressTime < maxTapDuration)//effective click 
            {
                if(move.magnitude < maxTapDistance)
                {
                    AttackInput(dir);
                    return;
                }
                else
                {
                    OnTouchMoveEnd(dir);
                }
                
            }
            else//release finger
            {
                OnTouchMoveEnd(dir);
            }
            return;
        }
        if(Input.GetMouseButton(0))
        {
            OnTouchMove(dir);
        }

    }

    public void OnTouchMove(Vector2 move)
    {
        //Debug.LogError("OnTouchMove");
        float directionIndegree = ChangeOrientation(move);
        Character.DirectionCommand message = new Character.DirectionCommand();
        message.character = character;
        message.direction = directionIndegree;
        character.onDirectionCommand.SendEventMessage(message);
    }

    void OnTouchMoveEnd(Vector2 move)
    {
        //Debug.LogError("OnTouchMoveEnd");
        float directionIndegree = ChangeOrientation(move);        
        Character.DirectionCommandEnd message = new Character.DirectionCommandEnd();
        message.character = character;
        message.direction = directionIndegree;
        character.onDirectionCommandEnd.SendEventMessage(message);
    }

    public void AttackInput(Vector2 move)
    {
        //ChangeOrientation(move);
        //OnTouchMoveEnd(screenPosition);

        Character.ButtonCommandEnd message = new Character.ButtonCommandEnd();
        message.character = character;
        message.button = ButtonType.Attack;
        character.onButtonCommandEnd.SendEventMessage(message);

    }

    float ChangeOrientation(Vector2 screenPosition)
    {
        Camera cam = Camera.main;
        float angle = Mathf.Atan2( screenPosition.x,screenPosition.y);
        float directionIndegree = cam.transform.rotation.y + angle* Mathf.Rad2Deg;
        //character.FaceTotTargetRotation(directionIndegree);
        return directionIndegree;
    }

    public void EvadeInput(Vector2 move)
    {
		//Debug.LogError("EvadeInput :" + move);
		{
	        Character.ButtonCommandEnd message = new Character.ButtonCommandEnd();
	        message.character = character;
	        message.button = ButtonType.Evade;
	        character.onButtonCommandEnd.SendEventMessage(message);
		}

		float directionIndegree = ChangeOrientation(move);
		{
			Character.DirectionCommand message = new Character.DirectionCommand();
			message.character = character;
			message.direction = directionIndegree;
			character.onDirectionCommand.SendEventMessage(message);
		}
    }

    public void WeaponSkillInput()
    {
        Debug.LogError("touch screen WeaponSkillInput");
        Character.ButtonCommandEnd message = new Character.ButtonCommandEnd();
        message.character = character;
        message.button = ButtonType.WeaponSpecial;
        character.onButtonCommandEnd.SendEventMessage(message);
    }
}


